package org.NooLab.itexx.app.gui.handlers;

import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.app.controller.SessionManager;
import org.NooLab.itexx.app.gui.StackedPanel;
import org.NooLab.itexx.app.gui.StackedPanelProducer;
import org.NooLab.itexx.app.gui.definition.ITexxWindowGuiIntf;
import org.NooLab.itexx.app.gui.handlers.keys.Keyhandlers;
import org.NooLab.utilities.logging.PrintLog;

public class ResultPanelMouseHandling {

	private ITexxWindowCommons appCommons;
	private SessionManager sessionMgr;
	
	/** the object that host the physical panel */
	StackedPanel parentObj;
	private StackedPanelProducer panelProducer;

	private long clickTime;
	private long lastClickTime;
	private boolean isDoubleClick;
	private Keyhandlers keyHandlers;
	private ITexxWindowGuiIntf windowGui;
	
	
	long cliccDT= 999999990L ;
	int functionKeyClick = -1 ;
	
	PrintLog out;
	private boolean stillAllowed,rightClick,daIsRunning=false;
	
	DelayedAction delayedAct ; 
	
	
	// ========================================================================	
	public ResultPanelMouseHandling(StackedPanel parentObj){
		this.parentObj = parentObj;
		
		panelProducer = parentObj.getPanelProducer() ;
		appCommons = panelProducer.getAppCommons();
		out = appCommons.out ;
		
		windowGui = appCommons.getWindowGui() ;
		sessionMgr = appCommons.getSessionManager();
		keyHandlers = appCommons.getKeyHandlers() ;
	}
	// ========================================================================

	
	public void handling(int panelIndex, MouseEvent evt) {
		// we have to delay the reaction to the click, it could be a double click, a ctrl-click, right click etc

		isDoubleClick = false;
		rightClick = evt.getButton() == 2;
		clickTime = System.currentTimeMillis();
		cliccDT = clickTime - lastClickTime;
		functionKeyClick = -1;
		lastClickTime = clickTime ;
		
		if (clickTime - keyHandlers.getFunctionKeyTime()<45){
			out.print(2, "function-key-click to panel <"+panelIndex+"> detected : "+ keyHandlers.getCurrentControlFunctionKey() );
			
			functionKeyClick = keyHandlers.getCurrentControlFunctionKey();
		}
		out.print(2, "delta T between clicks : "+cliccDT);
		
		rightClick=false;
		
		if ((evt.getButton()==3) || (evt.isPopupTrigger())){
			rightClick=true;
			
			// windowGui.getResultPanelPopupMenu().show(panelIndex,evt);
			
			return;
		}
		
		windowGui.setLastClickedPhysicalPanel(panelIndex); // from there, we will show activation effects
		
		if ((cliccDT>2) &&(cliccDT<380)){
			// clics = cliccCounter.detect( panelsTextPane ) ;
			isDoubleClick = true;
			out.print(2, "double-click to panel <"+panelIndex+"> detected. ");
			stillAllowed = false;
			// select panel or activate it
		}else{
		
			if (functionKeyClick==Keyhandlers._CFK_CTRL){
				parentObj.openClipboardChannel(-11) ;
			}else{
			// just open a small waiting process
				if (rightClick==false){
					stillAllowed=true;
					delayedAct = new DelayedAction(panelIndex);
					
				}else{
					stillAllowed=false;
					windowGui.showResultPanelPopup(panelIndex) ;
				}
			}
		}
		
	}
	
	public void performSingleClickAction(int param){
		parentObj.openClipboardChannel(11) ;
	}
	
	public void closeDelayedAction(){
		out.delay(10);
		delayedAct = null;
	}
	
	class DelayedAction implements Runnable {
		Thread daThrd;
		int param;
		
		
		public DelayedAction(int param) {
			this.param = param;
			daIsRunning=false;
			daThrd = new Thread(this,"daThrd") ;
			daThrd.start();
		}

		
		@Override
		public void run() {
			// 
			stillAllowed = true;
			daIsRunning=true;
			for (int i=0;i<39;i++){
				out.delay(10);
			}
			if (stillAllowed){
				performSingleClickAction(param);
			}else{
				closeDelayedAction();
			}
		}
		
		
	}


}
